const Luck = require("./luck.js");
Page({
  data: {
    width: '',
    height: '',
    random: null,
    lottery: '',
    activeIdx: 0,
    list: [],
    moveParams: {
      scrollLeft: 0
    }
  },
  onLoad() {
    let that = this;
    this.luck = new Luck(this);
    this.queryCategory();

    if (!wx.cloud) {
      wx.redirectTo({
        url: '../chooseLib/chooseLib',
      })
      return
    }

    wx.getSetting({
      success: res => {
        if (!res.authSetting['scope.userInfo']) {
          wx.redirectTo({ url: '/pages/auth/auth' })
        }
      }
    })

    wx.getSystemInfo({
      success: function (res) {
        let moveParams = that.data.moveParams
        moveParams.screenHalfWidth = res.screenWidth / 2;
        that.setData({
          systemInfo: res
        })
      },
    })
  },

  onGetUserInfo: function (e) {
    if (!this.logged && e.detail.userInfo) {
      this.setData({
        logged: true,
        avatarUrl: e.detail.userInfo.avatarUrl,
        userInfo: e.detail.userInfo
      })
    }
  },

  choseNav: function (e) {
    let ele = 'ele' + e.target.dataset.index
    this.setData({
      activeIdx: e.target.dataset.index,
      list: this.data.queryResult[e.target.dataset.index].products
    });
    this.getRect('#' + ele);
    this.reset();
    console.log(this.data);
  },

  getRect(ele) {
    var that = this;
    wx.createSelectorQuery().select(ele).boundingClientRect(function (rect) {
      let moveParams = that.data.moveParams;
      moveParams.subLeft = rect.left;
      moveParams.subHalfWidth = rect.width / 2;
      that.moveTo();
    }).exec()
  },

  scrollMove(e) {
    let moveParams = this.data.moveParams;
    moveParams.scrollLeft = e.detail.scrollLeft;
    this.setData({
      moveParams: moveParams
    })
  },

  moveTo: function () {
    let subLeft = this.data.moveParams.subLeft;
    let screenHalfWidth = this.data.moveParams.screenHalfWidth;
    let subHalfWidth = this.data.moveParams.subHalfWidth;
    let scrollLeft = this.data.moveParams.scrollLeft;
    let distance = subLeft - screenHalfWidth + subHalfWidth;
    scrollLeft = scrollLeft + distance;
    this.setData({
      scrollLeft: scrollLeft
    })
  },

  // 查询
  queryCategory() {
    const db = wx.cloud.database();
    db.collection('categories').where({
      _openid: this.data.openid
    }).get({
      success: res => {
        this.setData({
          queryResult: res.data,
          list: res.data[0].products
        })
        this.randomNum(0, this.data.list.length - 1);
        console.log('[数据库] [查询记录] 成功: ', res);
      },
      fail: err => {
        wx.showToast({
          icon: 'none',
          title: '查询记录失败'
        })
        console.error('[数据库] [查询记录] 失败：', err)
      }
    })
  },

  reset() {
    this.luck = new Luck(this);
    this.randomNum(0, this.data.list.length - 1);
  },
  randomNum(minNum, maxNum) {
    let random = parseInt(Math.random() * (maxNum - minNum + 1) + minNum);
    this.setData({
      lottery: this.data.list[random]
    })
  }
})